package v1

import (
	"chat/internal/common/request"
	"chat/internal/common/response"
	"chat/internal/service"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func GetAllGroupMember(c *gin.Context) {
	idStr, _ := c.GetQuery("uid")
	id, _ := strconv.Atoi(idStr)
	list, err := service.GetAllGroupMember(id)
	if err == nil {
		c.JSON(http.StatusOK, response.SuccessMsg(list))
		return
	}
	c.JSON(http.StatusOK, response.FailMsg("GetAllGroupMember failed"))
	return
}

func JoinGroup(c *gin.Context) {
	var req request.JoinGroupRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		c.JSON(http.StatusOK, response.FailMsg("获取参数失败"))
		return
	}
	cnt, err := service.GetGroupCntByGroupId(req.GroupId)
	if err != nil {
		c.JSON(http.StatusOK, response.FailMsg("service.GetGroupCntByGroupId err"))
		return
	}
	if cnt == 0 {
		c.JSON(http.StatusOK, response.FailMsg("该群不存在"))
		return
	}
	err = service.AddMember(req.Uid, req.GroupId)
	if err != nil {
		c.JSON(http.StatusOK, response.FailMsg("service.AddMember"))
		return
	}
	c.JSON(http.StatusOK, response.SuccessMsg(nil))
}
